package cn.lijian.goods.admin.admin.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.lijian.goods.admin.admin.domain.Admin;
import cn.lijian.goods.admin.admin.service.AdminService;

public class AdminServlet extends BaseServlet {

	private AdminService adminService=new AdminService();
	/**
	 * 退出功能
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String quit(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getSession().invalidate();// 销毁session
		return "r:/adminjsps/login.jsp";
	}
	
	/**
	 * 登录功能
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Admin form=CommonUtils.toBean(request.getParameterMap(), Admin.class);
		try {
			Admin admin=adminService.login(form);
			
			if(admin==null){
				request.setAttribute("msg", "用户名或密码错误");
				return "f:/adminjsps/login.jsp";
			}else{
				request.getSession().setAttribute("admin", admin);
				return "r:/adminjsps/admin/index.jsp";
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}

}
